package com.zyf.chapter02.structure.flyweight.eidtor.refactor;

import com.zyf.chapter02.structure.flyweight.eidtor.Font;

import java.util.ArrayList;
import java.util.List;

/**
 * 字体格式工厂
 * <p>
 * 享元模式的使用：通过工厂,对实现对享元的获取和初始化
 * @author yifeng
 */
public class CharacterStyleFactory {
    private static final List<CharacterStyle> styles = new ArrayList<>();

    public static CharacterStyle getStyle(Font font, int size, int colorRGB) {
        CharacterStyle newStyle = new CharacterStyle(font, size, colorRGB);
        for (CharacterStyle style : styles) {
            if (style.equals(newStyle)) {
                return style;
            }
        }
        styles.add(newStyle);
        return newStyle;
    }
}
